//
//  ByCellDayEarningApply.m
//  mijifen_ios
//
//  Created by mc on 2017/10/30.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellDayEarningApply.h"
@interface ByCellDayEarningApply()<UITextFieldDelegate>
@end


@implementation ByCellDayEarningApply
{
    NSString *_subTitle;
    NSString *_placeTitle;
    
    UIView      *_bgView;
    UIButton    *_subButton;
    
}

+ (instancetype)cellWithTableView:(UITableView*)tableView  andPlaceTitle:(NSString *)placeTitle andSubTitle:(NSString *) subTitle{
    
    static NSString *cellIdentifier = @"ByCellDayEarningApply";
    ByCellDayEarningApply *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier andPlaceTitle:placeTitle andSubTitle:subTitle];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andPlaceTitle:(NSString *)placeTitle andSubTitle:(NSString *) subTitle{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _subTitle = subTitle;
        _placeTitle = placeTitle;
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard) name:@"hideApplyKeyboard" object:nil];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20, 50));
    }];
    
    _subButton = [[UIButton alloc] init];
    [_bgView addSubview:_subButton];
    CGFloat width,height;
    if ([_subTitle isEqualToString:@"全部提现"]) {
        width = [_subTitle getSizeOfString:16].width;
        height = 30;
        [_subButton setTitle:_subTitle forState:UIControlStateNormal];
        [_subButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _subButton.titleLabel.font = FONT(16);
        [_subButton addTarget:self action:@selector(allApply) forControlEvents:UIControlEventTouchUpInside];
       
    }else if([_subTitle isEqualToString:@"Conjuration_arrow_down"]){
        width = height = 30;
        [_subButton setImage:[UIImage imageNamed:@"Conjuration_arrow_down"] forState:UIControlStateNormal];
    }else{
        width = height = 0;
    }
    [_subButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_bgView.mas_right).offset(-10);
        make.top.equalTo(_bgView.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, height));
    }];
    
    _textField = [[UITextField alloc] init];
    [_bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bgView.mas_top).offset(10);
        make.left.equalTo(_bgView.mas_left).offset(10);
        make.right.equalTo(_subButton.mas_left);
        make.bottom.equalTo(_bgView.mas_bottom).offset(-10);
    }];
    _textField.font = FONT(16);
    _textField.placeholder = _placeTitle;
    _textField.delegate = self;
   
    if ([_subTitle isEqualToString:@"Conjuration_arrow_down"]) {
        _textField.userInteractionEnabled = NO;
        _subButton.userInteractionEnabled = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(applyMethod)];
        [self addGestureRecognizer:tap];
    }
}

#pragma mark - 全部提现
- (void)allApply{
    if (self.allApplyBlock) {
        self.allApplyBlock();
    }
}
#pragma mark - 提现方式
- (void)applyMethod{
    if (self.applyMethodBlock) {
        self.applyMethodBlock();
    }
}

#pragma mark -
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.editBlock) {
        self.editBlock(textField.text);
    }
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if ([_subTitle isEqualToString:@"Conjuration_arrow_down"]) {
        return NO;
    }
    return YES;
}
#pragma mark -
- (void)hideKeyBoard{
    [_textField resignFirstResponder];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hideApplyKeyboard" object:nil];
}
@end
